from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import urllib2
from urllib2 import HTTPError, URLError
import BeautifulSoup as BS
from django.utils import simplejson

class MainPage(webapp.RequestHandler):
    
    
    def get(self):
        w = self.response.out
        #w.write('Hello, webapp World!')
        url=self.request.get('url')
        if(url!=None and url!=""):
            if not url.startswith("http://timesofindia.indiatimes.com"): self.redirect(url) 
            else:
                self.response.headers['Content-Type'] = 'text/html;charset=UTF-8'
                try:
                        request = urllib2.Request(url)
                        opener = urllib2.build_opener()   
                        request.add_header('User-Agent','TOIHEAD/1.0 +http://toihead.appspot.com/') 
                        rssfeed = opener.open(request).read()
                        rssfeed = unicode( rssfeed, "utf-8" )
                        bs= BS.BeautifulSoup()
                        bs.feed(rssfeed)
                        sdiv = bs.find('div', attrs={'id':'storydiv'})
                        title = bs.find('h1',attrs={'class':'heading'})
                        try:
                            title = title.string
                        except:
                            title="toihead"
                        sdiv=self.makeAscii(str(sdiv))
                        if sdiv==None:
                            sdiv=""
                        params = {'title':title,'content':sdiv,'url':url}
                        w.write(template.render("article.html", params))
                except HTTPError,e: 
                        print "Error:", e.code
                except URLError, e:
                        print 'We failed to reach a server.'
                        print 'Reason: ', e.reason
                except:
                    self.redirect(url,0)
        else:
            url = "http://search.twitter.com/trends.json"
            tweeturl= "http://search.twitter.com/search.json?q=from%3Atoihead"
            #result = urlfetch.fetch(url)
            try:
                request = urllib2.Request(tweeturl)
                opener = urllib2.build_opener()   
                request.add_header('User-Agent','TOIHEAD/1.0 +http://toihead.appspot.com/') 
                result2 = opener.open(request).read()
                #if result.status_code == 200:
                #    trends = simplejson.loads(result.content)
                tweets = simplejson.loads(result2)
                tweets = tweets['results']
            except:
                tweets ='';        
            w.write(template.render("index.html", {'tweets':tweets}))
                        
    def makeAscii(self,str):
        s = ""
        for char in str:
            if not (ord(char) < 32 or ord(char) > 127): s+=char
            else: s+=" "
        return s

application = webapp.WSGIApplication([('/', MainPage)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
